﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.Data.SqlClient;
using Jungle.Code.DAL;

namespace Jungle.Code.Core
{
	public class Template
	{
		#region Fields
		private int _templateID;
		private string _name;
		private string _basePath;
		private string _userTemplate;
		private string _adminTemplate;
		#endregion

		#region Properties
		public int TemplateID {
			get {
				return _templateID;
			}
			set {
				_templateID = value;
			}
		}
		public string Name {
			get {
				return _name;
			}
			set {
				_name = value;
			}
		}
		public string BasePath {
			get {
				return _basePath;
			}
			set {
				_basePath = value;
			}
		}
		public string UserTemplate {
			get {
				return _userTemplate;
			}
			set {
				_userTemplate = value;
			}
		}
		public string AdminTemplate {
			get {
				return _adminTemplate;
			}
			set {
				_adminTemplate = value;
			}
		}
		#endregion

		public static List<Template> GetAll() {
			List<Template> templates = new List<Template>();
			SqlConnection conn = new SqlConnection(DBHelper.ConnectionString);
			using (SqlCommand command = new SqlCommand("dbo.crTemplates_GetAll", conn)) {
				command.CommandType = CommandType.StoredProcedure;
				conn.Open();
				SqlDataReader dr = command.ExecuteReader(CommandBehavior.CloseConnection);
				if (dr.HasRows) {
					while (dr.Read()) {
						Template temp = new Template();
						SetAllFields(temp, dr);
						templates.Add(temp);
					}
				}
				dr.Close();
			}
			return templates;
		}

		public static Template GetByTemplateID(int templateID) {
			Template temp = new Template();
			SqlConnection conn = new SqlConnection(DBHelper.ConnectionString);
			using (SqlCommand command = new SqlCommand("dbo.crTemplates_GetByTemplateID", conn)) {
				command.CommandType = CommandType.StoredProcedure;
				command.Parameters.Add("templateID", SqlDbType.Int).Value = templateID;
				conn.Open();
				SqlDataReader dr = command.ExecuteReader(CommandBehavior.CloseConnection);
				if (dr.HasRows) {
					dr.Read();
					SetAllFields(temp, dr);
				}
				dr.Close();
			}
			return temp;
		}

		private static void SetAllFields(Template template, SqlDataReader dr) {
			template.TemplateID = (int)dr["templateID"];
			template.Name = (string)dr["name"];
			template.BasePath = (string)dr["basePath"];
			template.AdminTemplate = (string)dr["adminTemplate"];
			template.UserTemplate = (string)dr["userTemplate"];
		}

	}
}
